import { ASTResult } from '../types/ops'

export class RelatedSyntacticSugar {
  hasRelated(content: ASTResult): boolean | ASTResult {
    if (!content?.doc?.tags) return false
    const curComment = content.doc?.tags.find(tagItem => tagItem.title === 'related')
    if (!curComment?.description) {
      return false
    }
    return content
  }

  titleAndContent(content: ASTResult): [string, ASTResult] {
    const curComment = content.doc?.tags.find(tagItem => tagItem.title === 'related')
    return [curComment?.description as string, content]
  }

  add(map: Map<string, ASTResult[]>, title: string, cont: ASTResult) {
    const has = map.has(title)
    if (has) {
      const content = map.get(title)
      content?.push(cont)
      map.set(title, content as ASTResult[])
      return
    }
    map.set(title, [cont])
    return
  }
}
